package com.dcbeida.mine.detail;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.addnewpatient.AddNewPatientAct;
import com.dcbeida.activity.salsesdedail.SalsesDetailContract;
import com.dcbeida.activity.salsesdedail.SalsesDetailPresenter;
import com.dcbeida.adapter.MyClientDetailAdapter;
import com.dcbeida.bean.MyClientBean;
import com.dcbeida.bean.Pathology;
import com.dcbeida.bean.Regionname;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.utils.Constants;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 客户详情
 * Created by 150410 on 2017/9/18.
 */

public class MyClientDetailAct extends BaseActivity implements SalsesDetailContract.View {

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv_csny)
    TextView tvCsny;
    @Bind(R.id.tv_sfzh)
    TextView tvSfzh;
    @Bind(R.id.tv_jtzz)
    TextView tvJtzz;
    @Bind(R.id.tv_yyys)
    TextView tvYyys;
    @Bind(R.id.tv_xys)
    TextView tvXys;
    @Bind(R.id.tv_blfx)
    TextView tvBlfx;
    @Bind(R.id.tv_yyxs)
    TextView tvYyxs;
    @Bind(R.id.tv_scyys)
    TextView tvScyys;
    @Bind(R.id.myList)
    ListView myList;
    @Bind(R.id.tv_gmjl)
    TextView tvGmjl;
    private List<Object> datalist;
    private MyClientDetailAdapter adapter;
    private SalsesDetailPresenter mPresent;
    private MyClientBean myClientBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.my_client_detail;
    }

    @Override
    public void setData() {
        super.setData();
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            myClientBean = (MyClientBean) bundle.getSerializable("myClientBean");
        }
        imgBack.setVisibility(View.VISIBLE);
        tvGmjl.setVisibility(View.GONE);
        tvTitle.setText(R.string.khxq);
        tvRight.setText(R.string.bj);
        setClitentData();

        datalist = new ArrayList<Object>();
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new MyClientDetailAdapter(this);
        myList.setAdapter(adapter);
        mPresent = new SalsesDetailPresenter(this, this);
        mPresent.getSalsesDetailInfo(MyApplication.user.getUid(), "", myClientBean.getId() + "");
    }

    private void setClitentData() {


        tvName.setText(myClientBean.getTruename());
        if (1 == myClientBean.getSex()) {
            tv2.setText(getString(R.string.nan) + " " + myClientBean.getAge() + getString(R.string.sui) + " " + myClientBean.getPhonenum());
        } else if (2 == myClientBean.getSex()) {
            tv2.setText(getString(R.string.nv) + " " + myClientBean.getAge() + getString(R.string.sui) + " " + myClientBean.getPhonenum());
        } else {
            tv2.setText(getString(R.string.zx) + " " + myClientBean.getAge() + getString(R.string.sui) + " " + myClientBean.getPhonenum());
        }


        tvCsny.setText(myClientBean.getBirthday());//出生年月
        tvSfzh.setText(myClientBean.getIdcardnum());
        tvJtzz.setText(myClientBean.getAddress());
        tvYyys.setText(myClientBean.getHospitalname() + " " + myClientBean.getDoctorname());

        String blfx = "";//病理分析
        String yyxs = "";//用药线数
        Gson gson = new Gson();
        if (!TextUtils.isEmpty(myClientBean.getPathologystr())) {
            Pathology pathology = gson.fromJson(myClientBean.getPathologystr(), Pathology.class);

            List<Pathology.TypeBean> type = pathology.getType();
            if (null != type) {
                for (int i = 0; i < type.size(); i++) {
                    Pathology.TypeBean typeBean = type.get(i);
                    if (0 == i || i == type.size()) {
                        blfx += typeBean.getName();
                    } else {
                        blfx += "," + typeBean.getName();
                    }
                }
            }


            List<Pathology.SeverallinesBean> severallines = pathology.getSeverallines();
            if (null != severallines) {
                for (int i = 0; i < severallines.size(); i++) {
                    Pathology.SeverallinesBean bean = severallines.get(i);
                    if (0 == i || i == severallines.size()) {
                        yyxs += bean.getName();
                    } else {
                        yyxs += "," + bean.getName();
                    }
                }
            }
        }

        //地址
        if (!TextUtils.isEmpty(myClientBean.getRegionname())) {
            Regionname regionname = gson.fromJson(myClientBean.getRegionname(), Regionname.class);
            tvJtzz.setText(regionname.getProvince() + regionname.getCity() + regionname.getArea() + myClientBean.getAddress());
        }


        if (0 == myClientBean.getSmokestatus()) {
            //不吸烟
            tvXys.setText(getString(R.string.bxy));
        } else if (1 == myClientBean.getSmokestatus()) {
            //吸烟
            tvXys.setText(getString(R.string.xy) + myClientBean.getSmokeyears());
        } else if (2 == myClientBean.getSmokestatus()) {
            tvXys.setText(getString(R.string.yjy) + myClientBean.getSmokeyears());
        }

        tvBlfx.setText(blfx);
        tvYyxs.setText(yyxs);
        tvScyys.setText(myClientBean.getFirsttreatment());
    }


    @OnClick({R.id.img_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                Bundle bundle = new Bundle();
                bundle.putSerializable("myClientBean", myClientBean);
                bundle.putInt("operaterType",1);
                showActivityForResult(AddNewPatientAct.class, bundle, Constants.ONE);
                break;
        }
    }

    @Override
    public void setPresenter(SalsesDetailContract.Present presenter) {

    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void showSalsesDetailList(ArrayList<SalsesDetail.ListBean> list) {
        ArrayList<SalsesDetail.ListBean> tempList = new ArrayList<>();
        for (SalsesDetail.ListBean bean : list) {
            if (bean.getRefundid().equals("0")) {
                tempList.add(bean);
            }
        }
        adapter.setmArrayList(tempList);
        if (adapter.getCount() > 0) {
            tvGmjl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showList(ArrayList<MyClientBean> list) {
        myClientBean = list.get(0);
        setClitentData();
    }

    @Override
    public void showSignStatus(int signStatus) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.ONE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            if (null != bundle) {
                myClientBean = (MyClientBean) bundle.getSerializable("myClientBean");
                mPresent.getList(0, 1, myClientBean.getTruename(), "", "0", "");
            }
        }
    }
}
